function aptSafetyInit() {
    var advisedId = emosWS.getNewElementId();
    var languageTexts = {
        "T02_Safety": {
            id: "safetyHeader",
            text: "T02_Safety"
        },
        "T02_BoothSafety": {
            id: "safetyBoothText",
            text: "T02_BoothSafety"
        },
        "T02_AirPressureSafety": {
            id: "safetyAirPressureText",
            text: "T02_AirPressureSafety"
        },
        "T02_FireSafety": {
            id: "safetyFireText",
            text: "T02_FireSafety"
        },
        "T02_EmergencyStop": {
            id: "safetyEstopText",
            text: "T02_EmergencyStop"
        }
    };

    var opcTopic = sessionStorage.getItem("apt_PLC.OpcTopic");
    var boothTag = sessionStorage.getItem("apt_PLC.Tags.BoothSafety");
    var AirPressureTag = sessionStorage.getItem("apt_PLC.Tags.AirPressureSafety");
    var FireTag = sessionStorage.getItem("apt_PLC.Tags.Fire");
    var estopTag = sessionStorage.getItem("apt_PLC.Tags.EStopStation");

    function dataHandler() {
        emosWS.advise(opcTopic + boothTag, function(msg) {
            var container = $('#safetyBoothContainer');
            switch (msg.value) {
                case "2":
                    {
                        container.addClass("safetyStateOk").removeClass("safetyStateWarning safetyStateError safetyStateUnknown");
                        break;
                    }
                case "4":
                    {
                        container.addClass("safetyStateError").removeClass("safetyStateWarning safetyStateOk safetyStateUnknown");
                        break;
                    }
                case "8":
                    {
                        container.addClass("safetyStateWarning").removeClass("safetyStateError safetyStateOk safetyStateUnknown");
                        break;
                    }
                default:
                    {
                        container.addClass("safetyStateUnknown").removeClass("safetyStateWarning safetyStateOk safetyStateError");
                    }
            }
        }).setId(advisedId);

        emosWS.advise(opcTopic + AirPressureTag, function(msg) {
            var container = $('#safetyAirPressureContainer');
            switch (msg.value) {
                case "2":
                    {
                        container.addClass("safetyStateOk").removeClass("safetyStateWarning safetyStateError safetyStateUnknown");
                        break;
                    }
                case "4":
                    {
                        container.addClass("safetyStateError").removeClass("safetyStateWarning safetyStateOk safetyStateUnknown");
                        break;
                    }
                case "8":
                    {
                        container.addClass("safetyStateWarning").removeClass("safetyStateError safetyStateOk safetyStateUnknown");
                        break;
                    }
                default:
                    {
                        container.addClass("safetyStateUnknown").removeClass("safetyStateWarning safetyStateOk safetyStateError");
                    }
            }
        }).setId(advisedId);

        emosWS.advise(opcTopic + FireTag, function(msg) {
            var container = $('#safetyFireContainer');
            switch (msg.value) {
                case "2":
                    {
                        container.addClass("safetyStateOk").removeClass("safetyStateWarning safetyStateError safetyStateUnknown");
                        break;
                    }
                case "4":
                    {
                        container.addClass("safetyStateError").removeClass("safetyStateWarning safetyStateOk safetyStateUnknown");
                        break;
                    }
                case "8":
                    {
                        container.addClass("safetyStateWarning").removeClass("safetyStateError safetyStateOk safetyStateUnknown");
                        break;
                    }
                default:
                    {
                        container.addClass("safetyStateUnknown").removeClass("safetyStateWarning safetyStateOk safetyStateError");
                    }
            }
        }).setId(advisedId);

        emosWS.advise(opcTopic + estopTag, function(msg) {
            var container = $('#safetyEstopContainer');
            switch (msg.value) {
                case "2":
                    {
                        container.addClass("safetyStateOk").removeClass("safetyStateWarning safetyStateError safetyStateUnknown");
                        break;
                    }
                case "4":
                    {
                        container.addClass("safetyStateError").removeClass("safetyStateWarning safetyStateOk safetyStateUnknown");
                        break;
                    }
                case "8":
                    {
                        container.addClass("safetyStateWarning").removeClass("safetyStateError safetyStateOk safetyStateUnknown");
                        break;
                    }
                default:
                    {
                        container.addClass("safetyStateUnknown").removeClass("safetyStateWarning safetyStateOk safetyStateError");
                    }
            }
        }).setId(advisedId);
    }
    languageHandler(languageTexts); // Language handler in Durr.DXQ.APT.Library.js
    dataHandler();
}

// This iFrame is independend to the rest of the framework, thus we need to connect to the emosWS again. When connection is established, start initializing the window.
function initSafetyWindow() {
    emosWS.connect({ onopen: aptSafetyInit() });
}
